const formatArray = (arr = [],meta) => {
    const myArr = [];
    for (const obj of arr) {
        myArr.push(formatObject(obj,meta));
    }
    return myArr;
}

const formatObject = (obj = {},meta) => {
    for (const key in obj) {
        let value = obj[key];

        if(meta){
            for(let m of meta){//遍历数据库元数据
                if(m.name === key){//匹配结果集中的列名和元数据中的列名保持一致
                    if(m.type === 12){//type为12表示数据库的日期类型
                        value = formatDate(value);
                    }
                }
            }
        }

        if (key.includes("_")) {
            const keys = key.split("_");
            let k = keys[0];
            for (let i = 1; i < keys.length; i++) {
                let name = keys[i];
                let n = name.substring(0, 1).toUpperCase();
                let e = name.substring(1);
                name = n + e;
                k += name;
            }
            obj[k] = value;
            delete obj[key];
        }
    }
    return obj;
}

const formatDate=date=>{
    if(date){
        const year = date.getFullYear();
        const month = date.getMonth()+1;
        const day = date.getDate();
        const hours = date.getHours();
        const mins = date.getMinutes();
        const secs = date.getSeconds();

        return `${year}-${formatNumber(month)}-${formatNumber(day)} ${formatNumber(hours)}:${formatNumber(mins)}:${formatNumber(secs)}`
    }
    return null;
}

const formatNumber=num=>{
    return num < 10 ? '0'+num : num;
}
module.exports = {formatArray, formatObject,formatDate}
